/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.sql.DataSource;

/**
 *
 * @author krisada
 */
public class SearcherImpl implements Searcher {
    private String tableName;
    private String outColumns;
    private String[] whereColumns;
    private int[] columnTypes;
    private Connection connection;

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public ResultSet search(int offset, int limit, Object[] params) throws SQLException {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ").append(outColumns).append(" FROM ");
        sql.append(tableName);
        if( params.length > 0 ) {
            sql.append(" WHERE ");
        }
        if( whereColumns != null ) {
            for (int i = 0; i < whereColumns.length; i++) {
                if( columnTypes[i] == 0 ) {
                    if( i > 0 ) {
                        sql.append(" AND ");
                    }
                    sql.append(whereColumns[i]);
                    sql.append(" like ?");
                } else {
                    if( i > 0 ) {
                        sql.append(" AND ");
                    }
                    sql.append(whereColumns[i]);
                    sql.append(" = ?");
                }
            }
        }
        if( limit != -1 ) {
            sql.append(" LIMIT ? ");
        }
        if( offset != -1 ) {
            sql.append(" OFFSET ? ");
        }
        System.out.println("sql["+ sql.toString()+"]");
        PreparedStatement ps = connection.prepareStatement(sql.toString());
        if( whereColumns != null ) {
            for(int i = 0; i < whereColumns.length; i++) {
               switch( columnTypes[i] ) {
                   case 0:
                       String var = (String) params[i];
                       var = (var != null) ? var.replace('*', '%') : "";
                       ps.setString(i+1, var);
                       break;
                   case 1:
                       Integer ivar = (Integer) params[i];
                       ps.setInt(i+1, ivar.intValue());
                       break;
                   case 2:
                       Double dvar = (Double)params[i];
                       ps.setDouble(i+1, dvar.doubleValue());
                       break;
                   case 3:
                       java.util.Date dvat = (java.util.Date)params[i];
                       ps.setTimestamp(i+1, new Timestamp(dvat.getTime()));
                       break;
               }
            }
        }
        return ps.executeQuery();
    }
    

    public String getOutColumns() {
        return outColumns;
    }

    public void setOutColumns(String outColumns) {
        this.outColumns = outColumns;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String[] getWhereColumns() {
        return whereColumns;
    }

    public void setWhereColumns(String[] whereColumns) {
        this.whereColumns = whereColumns;
    }
    
    
}
